﻿Imports DotNetNuke
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Services.Search
Imports DotNetNuke.Services.Exceptions.Exceptions
Imports DotNetNuke.Common.Globals
Imports DotNetNuke.Common.Utilities.DataCache
Imports DotNetNuke.Entities.Users
Imports DotNetNuke.UI.Utilities
Imports System.Collections.Generic
Imports DotNetNuke.Common.Lists
Imports System.Activities.Statements


Namespace effority.UserDirectory

    Partial Class View
        Inherits effority.UserDirectory.Base
        Implements DotNetNuke.UI.Utilities.IClientAPICallbackEventHandler

#Region " Private Members "

        Private searchString As String

        'Private agentType As Enums.ClientAgentType = Enums.ClientAgentType.Browser

#End Region

#Region " Public Methods "

        Public Function DisplayEmail(ByVal Email As String) As String

            Return DotNetNuke.Common.Utilities.HtmlUtils.FormatEmail(Email)

        End Function


#End Region

#Region " Public Properties "

        ' Paging Settings
        Public ReadOnly Property paging() As Int32
            Get
                If Settings("paging") = "" Then
                    Return 20
                Else
                    Return CInt(Settings("paging"))
                End If
            End Get
        End Property

        Public ReadOnly Property HtmlNoResults() As String
            Get
                Return "<span class='Normal'>" & Localization.GetString("lblNoResults", Me.LocalResourceFile) & "</span>"
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Try
                ' modified by jie
                Dim sendTemp As String = "SendToUD_" & Me.searchText.UniqueID.Replace("$", "_") & "(1)"

                If Not IsPostBack Then

                    If Not Request.Params("searchString") Is Nothing Then

                        searchString = CStr(Request.Params("searchString"))
                        searchText.Text = searchString

                    End If

                    If Not Request.Params("Detail") Is Nothing AndAlso CStr(Request.Params("Detail")).ToLower = "true" Then

                        panOverview.Visible = False
                        panDetail.Visible = True

                        ShowDetail(CInt(Request.Params("UserId")))

                    Else

                        panOverview.Visible = True
                        panDetail.Visible = False

                    End If

                    'Do we need to add filtering dropdownlists?
                    For Each pd As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertyCollectionToFilter
                        'add a label, a dropdown, and a break
                        Dim lbl As New Label() With { _
                                .Text = Localization.GetString(pd.PropertyName & ".FilterLabel", LocalResourceFile), _
                                .Width = lblSearch.Width _
                            }

                        Dim ddl As New DropDownList()
                        ddl.ID = "FilterControl_" & pd.PropertyName
                        ddl.Width = searchText.Width
                        ddl.DataTextField = "Text"
                        ddl.DataValueField = "Value"
                        ddl.DataSource = New ListController().GetListEntryInfoCollection(pd.PropertyName)
                        ddl.DataBind()
                        ddl.Items.Insert(0, "")

                        ddl.Attributes.Add("onchange", sendTemp)
                        FilterControls.Add(ddl)

                        Dim br As New LiteralControl("<br />")

                        FilterPlaceHolder.Controls.Add(lbl)
                        FilterPlaceHolder.Controls.Add(ddl)
                        FilterPlaceHolder.Controls.Add(br)
                    Next

                    If Me.ShowProfilesToSearchSettings() Then

                        Dim columns As String

                        For i As Int32 = 0 To Me.ProfilesPropertyCollectionToSearch.Count - 1

                            columns &= Me.ProfilesPropertyCollectionToSearch(i).PropertyName

                            If Not i = Me.ProfilesPropertyCollectionToSearch.Count - 1 Then
                                columns &= ", "
                            End If

                        Next

                        lblSettings.Text = String.Format("({0} {1})", DotNetNuke.Services.Localization.Localization.GetString("SearchingIn", Me.LocalResourceFile), columns)
                        lblSettings.Visible = True

                    Else

                        lblSettings.Visible = False

                    End If

                    panGui.Visible = Not Me.DisableSearch

                    Dim dv As DataView = Me.GetUserTable(False)

                    Try

                        dv.RowFilter = Me.BuildSearchExpression(searchString, "")

                    Catch ex As Exception

                        ProcessModuleLoadException(Me, New Exception("Forced search expression error. Please check the module settings!"))

                    End Try


                    Try
                        If (Me.SearchStartMode = Enums.SearchStartMode.ShowAllRecords) _
                     Or (Not String.IsNullOrEmpty(searchString)) Then

                            HtmlAnker.InnerHtml = Me.BuildHtmlTable(dv, 1)

                        End If
                    Catch ex As Exception
                        ProcessModuleLoadException(Me, ex)
                    End Try

                End If

                Me.RegisterJSFunction()

                If IsXmlHttpEnabled Then

                    ClientAPI.RegisterClientReference(Me.Page, ClientAPI.ClientNamespaceReferences.dnn_xml)
                    ClientAPI.RegisterClientReference(Me.Page, ClientAPI.ClientNamespaceReferences.dnn_xmlhttp)

                    Me.searchText.Attributes.Add("onkeyup", sendTemp)
                End If

            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try

        End Sub

        Private FilterControls As New List(Of DropDownList)()

        Private ReadOnly Property IsXmlHttpEnabled As Boolean
            Get
                Return ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.XMLHTTP) _
                        AndAlso ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.XML)
            End Get
        End Property

#End Region

#Region " Private Methods "

        Private Sub RegisterJSFunction()

            Dim script As New System.Text.StringBuilder
            Dim filterData As String = ""

            For Each ddl As DropDownList In FilterControls
                If filterData.Length > 0 Then
                    filterData &= " + '#"
                Else
                    filterData = "'"
                End If
                ' format like "listname=slectedvalue#listname2=selectedvalue2"
                filterData &= ddl.ID.Split("_")(1) & "=' + " & " dnn.dom.getById('" & ddl.ClientID & "').options[ dnn.dom.getById('" & ddl.ClientID & "').selectedIndex ].value "
            Next

            ' modify by jie
            Dim searchTextId As String = Me.searchText.UniqueID.Replace("$", "_")
            ' Search
            Dim sendTemp As String = "function SendToUD_" & searchTextId & "(page) {"
            Dim successTemp As String = "function successFunc_" & searchTextId & "(result, ctx) {"
            Dim errorTemp As String = "function errorFunc_" & searchTextId & "(result, ctx) {"

            ' Sort
            Dim sortTemp As String = "function Sort_" & searchTextId & "(column) {"
            Dim successSortTemp As String = "function successSort_" & searchTextId & "(result, ctx) {"
            Dim errorSortTemp As String = "function errorSort_" & searchTextId & "(result, ctx) {"

            With script

                .Append("<script language=""javascript"">" & vbCrLf)
                .Append(sendTemp & vbCrLf)
                ' modify by jie
                If (String.IsNullOrEmpty(filterData)) Then
                    .Append(ClientAPI.GetCallbackEventReference(Me, "'1|1|' + dnn.dom.getById('" & searchTextId & "').value + '|' + page + '|'", "successFunc_" & searchTextId, "this", "errorFunc_" & searchTextId) & vbCrLf)
                Else
                    .Append(ClientAPI.GetCallbackEventReference(Me, "'1|1|' + dnn.dom.getById('" & searchTextId & "').value + '|' + page + '|' + " & filterData, "successFunc_" & searchTextId, "this", "errorFunc_" & searchTextId) & vbCrLf)
                End If

                .Append("}" & vbCrLf)

                .Append(successTemp & vbCrLf)
                .Append("dnn.dom.getById('" & HtmlAnker.UniqueID.Replace("$", "_") & "').innerHTML = result }" & vbCrLf)

                .Append(errorTemp & vbCrLf)
                .Append("}" & vbCrLf)

                .Append(sortTemp & vbCrLf)

                .Append(ClientAPI.GetCallbackEventReference(Me, "'2|1|'+ dnn.dom.getById('" & searchTextId & "').value + '||' + column", "successSort_" & searchTextId, "this", "errorSort_" & searchTextId) & vbCrLf)

                .Append("}" & vbCrLf)

                .Append(successSortTemp & vbCrLf)

                .Append("dnn.dom.getById('" & HtmlAnker.UniqueID.Replace("$", "_") & "').innerHTML = result }" & vbCrLf)

                .Append(errorSortTemp & vbCrLf)
                .Append("}" & vbCrLf)

                .Append("</script>" & vbCrLf)

            End With

            ' modified by jie
            Page.RegisterClientScriptBlock("SendToUD_" + searchTextId, script.ToString)

        End Sub

        Private Function GetUserTable(ByVal FromCache As Boolean) As DataView

            Try
                ' modified by jie
                Dim UserTable As DataTable
                If FromCache Then
                    UserTable = CType(DotNetNuke.Services.Cache.CachingProvider.Instance.GetItem(Me.USERTABLE_MODULE_CACHE), DataTable)
                End If

                If UserTable Is Nothing Then

                    UserTable = New System.Data.DataTable

                    UserTable.BeginLoadData()

                    Dim profilePropertyCollection As DotNetNuke.Entities.Profile.ProfilePropertyDefinitionCollection = _
                               DotNetNuke.Entities.Profile.ProfileController.GetPropertyDefinitionsByPortal(PortalId)

                    ' load users 
                    Dim UserListe As Generic.List(Of effority.UserDirectory.UserIdAndUserName)

                    UserListe = Me.GetUsersList()

                    'load Users in extended role
                    Dim ExtendedRoleUsers = GetExtendedRoleUsers()

                    ' load profile data 

                    Dim userProfileDic As Generic.Dictionary(Of Integer, Generic.Dictionary(Of Integer, String))

                    userProfileDic = effority.UserDirectory.Controller.GetUserProfileDic(Me.ProfilesPropertiesToLoad, PortalId)

                    UserTable.Columns.Add(effority.UserDirectory.Consts.USERID_COLUMN, GetType(Integer))

                    ' build dataTable
                    For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertiesToLoad

                        UserTable.Columns.Add(Me.CleanColumnName(info.PropertyName))

                    Next

                    For Each user As effority.UserDirectory.UserIdAndUserName In UserListe

                        Dim tUser As UserIdAndUserName = user
                        Dim showExtendeRoleProfiles As Boolean
                        If UseExtendedRole Then
                            'we're using the extended role, so only show extended role profile properties if the user is in that role
                            showExtendeRoleProfiles = ExtendedRoleUsers.Exists(Function(o As UserIdAndUserName) o.UserId = tUser.UserId)
                        Else
                            'extended role not used: always show property
                            showExtendeRoleProfiles = True
                        End If

                        Dim workrow As System.Data.DataRow = UserTable.NewRow()

                        workrow(effority.UserDirectory.Consts.USERID_COLUMN) = user.UserId

                        For Each info As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertiesToLoad
                            Dim showPropertyValue As Boolean

                            'always show if showExtendeRoleProfiles is true
                            If showExtendeRoleProfiles Then
                                showPropertyValue = True
                            Else
                                'otherwise: this user doesn't get extended role properties, so we need to check
                                Dim extendedRoleOnly As Boolean
                                If Not ExtendedRoleOnlyDic.TryGetValue(info.PropertyDefinitionId, extendedRoleOnly) Then
                                    extendedRoleOnly = False
                                End If
                                'show the property if it's not an extended role property
                                showPropertyValue = Not extendedRoleOnly
                            End If

                            If showPropertyValue Then
                                Dim value As String = String.Empty

                                If info.PropertyDefinitionId = Consts.USERNAME_PROFILE_ID Then
                                    value = user.UserName
                                ElseIf info.PropertyDefinitionId = Consts.EMAIL_PROFILE_ID Then
                                    value = user.Email
                                ElseIf info.PropertyDefinitionId = Consts.DISPLAYNAME_PROFILE_ID Then
                                    value = user.DisplayName
                                    'photo url
                                ElseIf info.PropertyDefinitionId = Consts.PHOTOURL_PROFILE_ID Then
                                    value = UserController.GetUserById(PortalId, user.UserId).Profile.PhotoURL
                                Else
                                    ' we need to try and get it from the profileproperties
                                    Dim profileForThisUser As Generic.Dictionary(Of Integer, String)
                                    If userProfileDic.TryGetValue(user.UserId, profileForThisUser) Then
                                        If profileForThisUser.TryGetValue(info.PropertyDefinitionId, Nothing) Then
                                            value = profileForThisUser(info.PropertyDefinitionId)
                                        Else
                                            If Not String.IsNullOrEmpty(info.DefaultValue) Then
                                                value = info.DefaultValue
                                            End If
                                        End If
                                    End If
                                End If

                                If HtmlDecodeDic.TryGetValue(info.PropertyDefinitionId, Nothing) Then

                                    If HtmlDecodeDic(info.PropertyDefinitionId) Then

                                        value = Server.HtmlDecode(value)

                                    End If

                                End If

                                workrow(CleanColumnName(info.PropertyName)) = value

                            End If

                        Next

                        UserTable.Rows.Add(workrow)

                    Next

                    UserTable.EndLoadData()

                    DotNetNuke.Services.Cache.CachingProvider.Instance.Insert(Me.USERTABLE_MODULE_CACHE, UserTable, False)

                End If

                Dim dv As DataView = UserTable.DefaultView

                dv.Sort = Me.DefaultSortExpression
                Return dv

            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try

        End Function

        Private Function GetExtendedRoleUsers() As List(Of effority.UserDirectory.UserIdAndUserName)
            Dim retval As New List(Of effority.UserDirectory.UserIdAndUserName)()

            If UseExtendedRole Then
                Dim RoleConfig As New Generic.Dictionary(Of Int32, Enums.RoleStatus)()
                RoleConfig.Add(ExtendedRoleId, effority.UserDirectory.Enums.RoleStatus.Add)
                retval.AddRange(effority.UserDirectory.Controller.GetUsersInRoles(RoleConfig, False))
            End If

            Return retval

        End Function

        Private Function GetUsersList() As Generic.List(Of effority.UserDirectory.UserIdAndUserName)

            Dim liste As Generic.List(Of effority.UserDirectory.UserIdAndUserName)

            liste = effority.UserDirectory.Controller.GetUsersInRoles(Me.RolesConfiguration, Me.IgnoreEffectiveExpireDate)

            Return liste

        End Function

        Private Sub ShowDetail(ByVal uId As Int32)

            Dim user As UserInfo = UserController.GetUser(PortalId, uId, True)

            Dim sb As New StringBuilder

            Dim info As New effority.UserDirectory.DetailInfo(user)

            Dim searchString As String = CStr(Request.Params("searchstring"))

            'Dim template As String = Me.detailsTemplate.Replace("[", "[User:")

            sb.Append(info.Replace(Me.detailsTemplate))

            Dim returnURL As String = DotNetNuke.Common.Globals.NavigateURL()

            If Not String.IsNullOrEmpty(Me.Session(Me.SESSION_LASTSEARCH)) Then

                returnURL &= "?searchString=" & searchString

            End If

            sb.Append("<div align='center'><a class='NormalBold' href='" & returnURL & "' target='_self'>" & DotNetNuke.Services.Localization.Localization.GetString("Back", Me.LocalResourceFile) & "</a></div>")

            lblDetail.Text = sb.ToString()

        End Sub

#End Region

#Region " Client API "

        Public Function RaiseClientAPICallbackEvent(ByVal eventArgument As String) As String Implements DotNetNuke.UI.Utilities.IClientAPICallbackEventHandler.RaiseClientAPICallbackEvent

            ' Zustand der GUI ermitteln 

            Dim strArray As String() = eventArgument.Split("|")

            ' param 0 = CallBackMode; Search or sort
            ' param 1 = SearchMode; fix or custom ; ausgestorben
            ' param 2 = search expression  
            ' param 3 = page
            ' param 4 = sortColumn

            Dim callBackMode As effority.UserDirectory.Enums.CallBackMode = CType(strArray(0), effority.UserDirectory.Enums.CallBackMode)
            Dim searchMode As effority.UserDirectory.Enums.SearchMode = CType(strArray(1), effority.UserDirectory.Enums.SearchMode)

            Dim searchExpression As String

            Dim searchString As String = strArray(2)
            Dim filterString As String = strArray(4)
            searchExpression = Me.BuildSearchExpression(searchString, filterString)

            Dim dv As DataView = Me.GetUserTable(True)

            dv.RowFilter = searchExpression

            Dim page As Int32

            If Not String.IsNullOrEmpty(strArray(3)) Then
                page = CInt(strArray(3))
                Session(SESSION_PAGE) = CStr(page)
            Else
                If Not Session(SESSION_PAGE) Is Nothing Then
                    page = CInt(Session(SESSION_PAGE))
                Else
                    page = 1
                End If
            End If

            If callBackMode = effority.UserDirectory.Enums.CallBackMode.sort Then

                Dim sortColumn As String = CStr(strArray(4))

                If CStr(Session(SESSION_SORT_EXPRESSION)) = sortColumn & " ASC" Then
                    Session(SESSION_SORT_EXPRESSION) = sortColumn & " DESC"
                Else
                    Session(SESSION_SORT_EXPRESSION) = sortColumn & " ASC"
                End If

                'Else

                '    If Session(SESSION_SORT_EXPRESSION) Is Nothing OrElse _
                '    String.IsNullOrEmpty(Session(SESSION_SORT_EXPRESSION)) Then

                '        Session(SESSION_SORT_EXPRESSION) = Me.DefaultSortExpression

                '    End If

            End If

            If Not Session(SESSION_SORT_EXPRESSION) Is Nothing Then

                dv.Sort = CStr(Session(SESSION_SORT_EXPRESSION))

            Else

                'If dv.Table.Columns.Count > 0 Then

                '    dv.Sort = dv.Table.Columns(0).ColumnName & " ASC"

                'End If

                dv.Sort = Me.DefaultSortExpression

            End If

            If searchString.Length >= Me.MinChar Or Me.SearchStartMode = Enums.SearchStartMode.ShowAllRecords Then

                Return Me.BuildHtmlTableBrowser(dv, page)

            Else

                Return "<span class='Normal'>No Results</span>"

            End If

        End Function

        Private Function BuildHtmlTable(ByVal dv As DataView, ByVal selPage As Int32) As String

            Dim result As String = String.Empty

            Dim agentType As Enums.ClientAgentType = Enums.ClientAgentType.Browser

            If Utils.AnalyseClientAgent(Me.Request.UserAgent) = Enums.ClientAgentType.Browser Then
                result = BuildHtmlTableBrowser(dv, selPage)
            Else
                result = BuildHtmlTableSearchBot(dv)
            End If

            Return result

        End Function

        Private Function BuildHtmlTableBrowser(ByVal dv As DataView, ByVal selPage As Int32) As String

            Dim CssHead As String = "NormalBold"
            Dim CssBody As String = "Normal"

            Try

                If dv Is Nothing OrElse dv.Count = 0 Then

                    Return Me.HtmlNoResults

                Else

                    Dim count As Int32 = dv.Count

                    Dim pages As Int32

                    If ((count Mod Me.paging) <= (Me.paging / 2)) And (count Mod Me.paging <> 0) Then
                        'wird aufgerundet

                        pages = count / Me.paging
                        pages = pages + 1

                    Else

                        pages = count / Me.paging

                    End If

                    Dim sb As New System.Text.StringBuilder

                    sb.Append("<table cellspacing='2' cellpadding='2'>")

                    If Me.DisplayMode = Enums.DisplayMode.Grid Then

                        sb.Append("<tr>")

                        For Each profileProperty As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertyCollectionToShow

                            Dim tempName As String = Me.ProfilePropertyNamesLocalDic(profileProperty.PropertyName)

                            sb.Append(String.Format("<td nowrap style='cursor:pointer' onclick='Sort_{3}(""{0}"")' class='{1}'>{2}</td>", CleanColumnName(profileProperty.PropertyName), CssHead, tempName, Me.searchText.UniqueID.Replace("$", "_")))

                        Next

                        sb.Append("</tr>")

                    ElseIf Me.DisplayMode = Enums.DisplayMode.Template Then

                        Dim info As New effority.UserDirectory.ProfilePropertyLocalization(Me.ProfilePropertyNamesLocalDic)

                        sb.Append(info.Replace(Me.DisplayHeadTemplate))

                    End If

                    'load Users in extended role
                    Dim ExtendedRoleUsers = GetExtendedRoleUsers()

                    For x As Int32 = 1 To pages

                        If x = selPage Then

                            For y As Int32 = 1 To Me.paging

                                ' page kommt vom client
                                If dv.Count > (((x - 1) * Me.paging) + (y - 1)) Then

                                    Dim z As Int32 = (((x - 1) * Me.paging) + (y - 1))

                                    If Me.DisplayMode = Enums.DisplayMode.Grid Then

                                        sb.Append("<tr>")

                                        For Each profileProperty As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertyCollectionToShow

                                            Dim mode As effority.UserDirectory.Enums.RenderMode = Enums.RenderMode.Text

                                            If Me.DisplayEmailAsMailTo AndAlso profileProperty.PropertyDefinitionId = effority.UserDirectory.Consts.EMAIL_PROFILE_ID Then
                                                mode = effority.UserDirectory.Enums.RenderMode.EmailMailTo
                                            End If

                                            ' Der ProfileLink sticht den EmailLink aus
                                            If (Me.EnableProfileLink AndAlso profileProperty.PropertyDefinitionId = Me.ProfileLinkColumn) Then
                                                mode = effority.UserDirectory.Enums.RenderMode.ProfileLink
                                            End If

                                            If (Me.EnablevCardLink AndAlso profileProperty.PropertyDefinitionId = Me.vCardLinkColumn) Then
                                                mode = effority.UserDirectory.Enums.RenderMode.vCardLink
                                            End If

                                            If (Me.detailsLinkEnable AndAlso profileProperty.PropertyDefinitionId = Me.detailsLinkColumn) Then
                                                mode = effority.UserDirectory.Enums.RenderMode.detailsView
                                            End If

                                            If (Me.TabLinkEnable AndAlso profileProperty.PropertyDefinitionId = Me.TabLinkColumn) Then
                                                mode = effority.UserDirectory.Enums.RenderMode.tabLink
                                            End If

                                            ' Profilewert bei Bedarf kürzen
                                            Dim value As String = Null.NullString
                                            'since we introduced extended role properties, the table value can be DBNull here:
                                            If Not dv(z).Item(CleanColumnName(profileProperty.PropertyName)).Equals(DBNull.Value) Then
                                                value = dv(z).Item(CleanColumnName(profileProperty.PropertyName))
                                            End If

                                            Dim localUserId As Integer = CInt(dv(z).Item(effority.UserDirectory.Consts.USERID_COLUMN))

                                            If MaxLengthDic.TryGetValue(profileProperty.PropertyDefinitionId, Nothing) Then

                                                Dim maxLength As Int32 = MaxLengthDic(profileProperty.PropertyDefinitionId)

                                                If maxLength > 0 Then

                                                    If value.Length > maxLength Then

                                                        value = value.Substring(0, maxLength)

                                                    End If

                                                End If

                                            End If

                                            Select Case mode

                                                Case effority.UserDirectory.Enums.RenderMode.ProfileLink
                                                    sb.Append("<td nowrap class='" & CssBody & "'><a href='" & DotNetNuke.Common.Globals.LinkClick("UserID=" & localUserId, Me.TabId, Me.ModuleId) & "' target='_self'>" & value & "</a></td>")

                                                Case effority.UserDirectory.Enums.RenderMode.EmailMailTo
                                                    sb.Append(String.Format("<td nowrap class='{0}'><a href='mailto:{1}'>{1}</a></td>", CssBody, value))

                                                Case effority.UserDirectory.Enums.RenderMode.Text
                                                    sb.Append(String.Format("<td nowrap class='{0}'>{1}</td>", CssBody, value))

                                                Case Enums.RenderMode.vCardLink
                                                    sb.Append("<td nowrap class='" & CssBody & "'><a href='" & Me.GetvCardLink(localUserId) & "' target='_self'>" & value & "</a></td>")

                                                Case Enums.RenderMode.detailsView
                                                    If UseExtendedRolePage AndAlso ExtendedRoleUsers.Exists(Function(o As UserIdAndUserName) o.UserId = localUserId) Then
                                                        sb.Append("<td nowrap class='" & CssBody & "'><a href='" & ExtendedRolePage.Replace("[USERID]", localUserId) & "' target='_self'>" & value & "</a></td>")
                                                    Else
                                                        sb.Append("<td nowrap class='" & CssBody & "'><a href='" & Me.GetDetailsViewLink(localUserId, searchText.Text) & "' target='_self'>" & value & "</a></td>")
                                                    End If

                                                Case Enums.RenderMode.tabLink

                                                    Dim localTabId As Int32 = 0

                                                    If Integer.TryParse(value, localTabId) AndAlso TabDictionary.TryGetValue(localTabId, Nothing) Then

                                                        Dim tabInf As DotNetNuke.Entities.Tabs.TabInfo = TabDictionary(localTabId)

                                                        sb.Append("<td nowrap class='" & CssBody & "'><a href='" & NavigateURL(localTabId) & "' target='" & Me.TabLinkTarget & "'>" & tabInf.TabName & "</a></td>")

                                                    Else

                                                        sb.Append("<td></td>")

                                                    End If

                                            End Select

                                        Next

                                        sb.Append("</tr>")

                                    ElseIf Me.DisplayMode = Enums.DisplayMode.Template Then


                                        Dim info As New effority.UserDirectory.DetailInfo(dv(z))

                                        info.UserId = CInt(dv(z).Item(effority.UserDirectory.Consts.USERID_COLUMN))

                                        info.RenderMode = Enums.ReplaceEngineRenderMode.Grid

                                        info.Base = Me

                                        sb.Append(info.Replace(Me.DisplayTemplate))

                                    End If

                                End If

                            Next

                        End If

                    Next

                    sb.Append("</table>")

                    Dim st As String
                    Dim searchTextId As String = Me.searchText.UniqueID.Replace("$", "_")
                    If pages > 1 AndAlso pages <= 20 Then

                        For i As Int32 = 1 To pages
                            ' modified by jie
                            If i = selPage Then
                                st = String.Format("<a href='javascript:void(0)' class='NormalBold' onclick=""SendToUD_{1}('{0}')"">{0}</a>&nbsp;", CStr(i), searchTextId)
                            Else
                                st = String.Format("<a href='javascript:void(0)' class='Normal' onclick=""SendToUD_{1}('{0}')"">{0}</a>&nbsp;", CStr(i), searchTextId)
                            End If

                            sb.Append(st)

                        Next
                    ElseIf pages > 20 Then

                        'sb.Append("<a href='javascript:void(0)' class='NormalBold' onclick=""SendToUD('1')"">&lt;&lt;</a>&nbsp;")

                        sb.AppendFormat("<a href='javascript:void(0)' class='NormalBold' onclick=""SendToUD_{1}('{0}')"">&lt;</a>&nbsp;", CStr(Math.Max(1, selPage - 1)), searchTextId)

                        For i As Int32 = Math.Max(1, selPage - 10) To Math.Min(pages, selPage + 10)

                            If i = selPage Then
                                sb.AppendFormat("<span class='NormalBold'>{0}</span>&nbsp;", CStr(i))
                            Else
                                sb.AppendFormat("<a href='javascript:void(0)' class='Normal' onclick=""SendToUD_{1}('{0}')"">{0}</a>&nbsp;", CStr(i), searchTextId)
                            End If

                        Next

                        sb.AppendFormat("<a href='javascript:void(0)' class='NormalBold' onclick=""SendToUD_{1}({0})"">&gt;</a>&nbsp;", CStr(Math.Min(pages, selPage + 1)), searchTextId)

                        sb.AppendFormat("<a href='javascript:void(0)' class='NormalBold' onclick=""SendToUD_{1}({0})"">&gt;&gt;</a>&nbsp;", CStr(pages), searchTextId)

                    End If

                    Return sb.ToString()

                End If

            Catch ex As Exception

                Return ex.ToString()

            End Try

        End Function

        Private Function BuildHtmlTableSearchBot(ByVal dv As DataView) As String

            Try

                Dim sb As New System.Text.StringBuilder

                If dv Is Nothing OrElse dv.Count = 0 Then

                    Return Me.HtmlNoResults

                Else

                    sb.Append("<table><tr>")

                    For Each profileProperty As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertyCollectionToShow

                        sb.Append(String.Format("<td>{0}</td>", profileProperty.PropertyName))

                    Next

                    sb.Append("</tr>")

                    For z As Integer = 0 To dv.Count - 1

                        sb.Append("<tr>")

                        For Each profileProperty As DotNetNuke.Entities.Profile.ProfilePropertyDefinition In Me.ProfilesPropertyCollectionToShow

                            sb.Append(String.Format("<td>{0}</td>", dv(z).Item(CleanColumnName(profileProperty.PropertyName))))

                        Next

                        sb.Append("</tr>")

                    Next

                End If

                sb.Append("</table>")

                Return sb.ToString()

            Catch ex As Exception

                'Return ex.ToString()

            Finally



            End Try

        End Function

#End Region

    End Class

End Namespace
